/*
* Created on: 2009-8-25
* Author: hxzon
*/
#include <jni.h>
#include "org_hxzon_demo_jni_JniDemo2.h"
#include <stdio.h>

jclass Class_MyObject;
jclass Class_java_lang_String;
jmethodID MID_String_init;
jmethodID MID_String_getBytes;

void initIDs(JNIEnv *env)
{
    Class_MyObject = env->FindClass("org/hxzon/demo/jni/MyObject");

    //Class_java_lang_String = env->FindClass("java/lang/String");

    //MID_String_init = env->GetMethodID(Class_java_lang_String,
    //    "<init>", "([B)V");

    //MID_String_getBytes = env->GetMethodID(Class_java_lang_String,
    //    "getBytes", "()[B");
}

/*
 * Class:     org_hxzon_demo_jni_JniDemo2
 * Method:    buildMyObject
 * Signature: (IJLjava/lang/String;[B)Lorg/hxzon/demo/jni/MyObject;
 */
JNIEXPORT jobject JNICALL Java_org_hxzon_demo_jni_JniDemo2_buildMyObject
  (JNIEnv *env, jobject thiz, jint myInt, jlong myLong, jstring myString, jbyteArray myArray)
{
    //init ids
    initIDs(env);

    //new myObj
    jobject myObj=env->AllocObject(Class_MyObject);

    //set myObj's myInt
    jfieldID fid = NULL;
    fid = env->GetFieldID(Class_MyObject, "myInt", "I");
    env->SetIntField(myObj,fid,myInt);

    //set myObj's myLong
    fid = env->GetFieldID(Class_MyObject, "myLong", "J");
    env->SetLongField(myObj,fid,myLong);

    //set myObj's myString
    fid = env->GetFieldID(Class_MyObject, "myString", "Ljava/lang/String;");
    env->SetObjectField(myObj,fid, myString);

    //set myObj's myString2
    fid = env->GetFieldID(Class_MyObject, "myString2", "Ljava/lang/String;");
    jstring myString2 = env->NewStringUTF("hello,i'am string2");
    env->SetObjectField(myObj,fid, myString2);

    //set myObj's myArray
    fid = env->GetFieldID(Class_MyObject,"myArray","[B");
    env->SetObjectField(myObj,fid,myArray);

    //set myObj's myBoolean
    fid = env->GetFieldID(Class_MyObject,"myBoolean","Z");
    env->SetBooleanField(myObj,fid,1);

    //return
    return myObj;
}